
package com.hlkj.pay.controller.common;

import java.util.Collections;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.common.CommonAppQueryDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.model.common.CommonAppDO;
import com.hlkj.pay.service.common.ICommonAppConfigService;
import com.hlkj.pay.vo.common.req.CommonAppPageQueryReq;
import com.hlkj.pay.vo.common.req.CommonAppReq;
import com.hlkj.pay.vo.common.req.CommonUpdateStatusReq;
import com.hlkj.pay.vo.common.req.DeleteIdReq;
import com.hlkj.pay.vo.common.resp.CommonAppResp;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "oauth2配置")
@RestController
@RequestMapping("/v1/common/app")
@RequiredArgsConstructor
@Slf4j
public class ComonAppConfigController extends BaseController {

    private final ICommonAppConfigService commonAppConfigService;

    private final com.hlkj.pay.app.common.ICommonConfigAppService commonConfigAppService;

    @Operation(summary = "分页查询")
    @GetMapping("/page")
    CommonResult<PageResult<CommonAppResp>> queryTemplatePage(@Validated CommonAppPageQueryReq pageParam) {
        CommonAppQueryDto queryDO = BeanUtil.copyProperties(pageParam, CommonAppQueryDto.class);
        queryDO.setLikeName(pageParam.getName());
        queryDO.setName(null);
        PageResult<CommonAppDO> pageResult = commonAppConfigService.queryPage(queryDO);
        List<CommonAppDO> list = pageResult.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(pageResult, Collections.emptyList());
        }
        List<CommonAppResp> templateResps = BeanUtil.copyToList(list, CommonAppResp.class);
        return buildPageResult(pageResult, templateResps);
    };

    @Operation(summary = "不分页查询")
    @GetMapping("/list")
    CommonResult<List<CommonAppResp>> queryTemplateList(@Validated CommonAppPageQueryReq pageParam) {
        CommonAppQueryDto queryDO = BeanUtil.copyProperties(pageParam, CommonAppQueryDto.class);
        List<CommonAppDO> list = commonAppConfigService.queryList(queryDO);
        if (CollectionUtils.isEmpty(list)) {
            return CommonResult.success(Collections.emptyList());
        }
        return CommonResult.success(BeanUtil.copyToList(list, CommonAppResp.class));
    };

    @Operation(summary = "详情查询")
    @GetMapping("/detail")
    CommonResult<CommonAppResp> detail(@RequestParam(name = "id") Long id) {
        CommonAppDO detail = commonAppConfigService.detail(id);
        if (detail == null) {
            return CommonResult.success(null);
        }
        return CommonResult.success(BeanUtil.copyProperties(detail, CommonAppResp.class));
    }

    @Operation(summary = "新增oauth2配置")
    @PostMapping("")
    CommonResult<String> addCommonAppConfig(@Validated @RequestBody CommonAppReq addCommonAppReq) {
        return commonConfigAppService.addCommonApp(addCommonAppReq);
    };

    @Operation(summary = "修改oauth2配置")
    @PutMapping("")
    CommonResult<Void> updateCommonAppConfig(@Validated @RequestBody CommonAppReq updateCommonAppReq) {
        return commonConfigAppService.updateCommonApp(updateCommonAppReq);
    };

    @Operation(summary = "删除oauth2配置")
    @DeleteMapping("")
    CommonResult<Void> deleteCommonAppConfig(@Validated @RequestBody DeleteIdReq deleteIdReq) {
        return commonConfigAppService.deleteCommonApp(deleteIdReq.getId());
    };

    @Operation(summary = "更新oauth2配置状态")
    @PutMapping("/status")
    CommonResult<Void> statusUpdate(@RequestBody CommonUpdateStatusReq commonUpdateStatusReq) {
        return commonConfigAppService.statusUpdate(commonUpdateStatusReq.getId(), CommonEnum.YES_NO_TYPE.from(commonUpdateStatusReq.getStatus()));
    };

}
